<template>
    <div class="mod-config">
        <div>
            <el-select v-model="value" placeholder="请选择" style="margin-right: 20px" @change="handleChange">
                <el-option-group
                        v-for="group in options"
                        :key="group.label"
                        :label="group.label">
                    <el-option
                            v-for="item in group.options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-option-group>
            </el-select>
            <!--    新增按钮      -->
            <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addPost">新增岗位</el-button>
            <el-form :inline="true" style="float:right">
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary" circle @click="search"></el-button>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="searchName" placeholder="搜索岗位名称例如：开发" size="medium" class="input" clearable="clearable"></el-input>
                </el-form-item>
            </el-form>
        </div>
        <el-divider></el-divider>
        <!--   主体：表格（基本信息，修改，删除）   -->
        <div class="table">
            <el-table :data="tableData" border style="width: 100%" v-loading="dataListLoading" >
                <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
                    <template #default="scope">
                        <span>{{ (currentPage - 1) * pageSize + scope.$index + 1 }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="名称" width="230" align="center"></el-table-column>
                <el-table-column prop="abbreviation" label="简称" width="230" align="center"></el-table-column>
                <el-table-column prop="organization.name" label="直属组织" width="230" align="center"></el-table-column>
                <el-table-column prop="createTime" label="创建日期" width="230" align="center"></el-table-column>
                <el-table-column prop="modifiedTime" label="修改日期" width="230" align="center"></el-table-column>
                <el-table-column fixed="right" label="操作" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button  size="small" icon="el-icon-edit" type="primary" @click="updatePost(scope.row.id)">
                            修 改
                        </el-button>
                        <el-button size="small" icon="el-icon-turn-off" type="danger" @click="disablePost(scope.row.id)">
                            禁 用
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    align="left"
                    @current-change="changePage"
                    background
                    layout="total, prev, pager, next"
                    :page-size="pageSize"
                    :current-page="currentPage"
                    :total="total">
            </el-pagination>
            <!--   弹窗-新增字典数据  -->
            <el-dialog title="新增岗位" :visible.sync="editFormVisibleForAddPostData" :show-close="isClose" :close-on-click-modal="isClose" center>
                <el-divider><i class="el-icon-setting"></i></el-divider>
                <el-form :model="addPostForm" :rules="addPostFormRules"  label-width="0px" class="demo-ruleForm">
                    <el-form-item prop="name" label="">
                        <el-input style="font-size: 16px;" placeholder="请填写岗位名称" v-model="addPostForm.name">
                            <template slot="prepend"><i class="el-icon-edit"></i>名称</template>
                        </el-input>
                    </el-form-item>
                    <div style="height: 20px"></div>
                    <el-form-item prop="abbreviation" label="">
                        <el-input style="font-size: 16px;" placeholder="请填写岗位简称" v-model="addPostForm.abbreviation">
                            <template slot="prepend"><i class="el-icon-edit"></i>简称</template>
                        </el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editFormVisibleForAddPostData=false">取 消</el-button>
                    <el-button type="primary" @click="comitPost">确 定</el-button>
                </div>
            </el-dialog>
            <!--   弹窗-修改岗位数据  -->
            <el-dialog title="修改岗位" :visible.sync="editFormVisibleForUpdatePostData" :show-close="isClose" :close-on-click-modal="isClose" center>
                <el-divider><i class="el-icon-setting"></i></el-divider>
                <el-form :model="updatePostForm" :rules="updatePostFormRules"  label-width="0px" class="demo-ruleForm">
                    <el-form-item prop="name" label="">
                        <el-input style="font-size: 16px;" placeholder="请填写岗位名称" v-model="updatePostForm.name">
                            <template slot="prepend"><i class="el-icon-edit"></i>名称</template>
                        </el-input>
                    </el-form-item>
                    <div style="height: 20px"></div>
                    <el-form-item prop="abbreviation" label="">
                        <el-input style="font-size: 16px;" placeholder="请填写岗位简称" v-model="updatePostForm.abbreviation" :disabled="true">
                            <template slot="prepend"><i class="el-icon-view"></i>简称</template>
                        </el-input>
                    </el-form-item>
                    <div style="height: 20px"></div>
                    <el-form-item prop="organizationId" label="">
                        <span style="font-size: 16px;">请选择所属组织：</span>
                        <el-select v-model="updatePostForm.organizationId" placeholder="请选择">
                            <el-option-group
                                v-for="group in options"
                                :key="group.label"
                                :label="group.label">
                                <el-option
                                    v-for="item in group.options"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-option-group>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="editFormVisibleForUpdatePostData=false">取 消</el-button>
                    <el-button type="primary" @click="updatePostData">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>
<script>
import dayjs from "dayjs";

export default {
    data() {
        return {
            //是否为搜索状态
            isSearch: false,
            // 当前页
            currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
            // 一共多少页
            pageSize: 10,
            // 一共多少条
            total: 10,
            // 加载
            dataListLoading:false,
            //表格数据
            tableData:[],
            //下拉框数据
            options: [],
            // 下拉框选中值
            value: '',
            //搜索的字典名称
            searchName:'',
            //当前组织
            curOrganization:'',
            // 弹窗-新增岗位
            editFormVisibleForAddPostData:false,
            //修改岗位数据
            editFormVisibleForUpdatePostData:false,
            //是否可显示关闭
            isClose:false,
            //新增岗位表单
            addPostForm:{
                enable:true,
                name:'',
                abbreviation:'',
                organizationId:''
            },
            //新增岗位表单验证
            addPostFormRules:{
                name: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' },
                ],
                abbreviation: [
                    { required: true, message: '请输入岗位简称', trigger: 'blur' },
                ],
            },
            //修改岗位数据表单
            updatePostForm:{
                id:'',
                enable:true,
                name:'',
                abbreviation:'',
                organizationId:''
            },
            //修改岗位数据表单验证
            updatePostFormRules:{
                name: [
                    { required: true, message: '请输入岗位名称', trigger: 'blur' },
                ],
                abbreviation: [
                    { required: true, message: '请输入岗位简称', trigger: 'blur' },
                ]
            }
        }
    },
    mounted() {
        this.isSearch= false;
        this.loadOptions();
    },
    methods: {
        dayjs,
        postInfo(e,message){
            if(e.name === '' || e.name === null){
                e.name = message;
            }
            if(e.abbreviation === '' || e.abbreviation === null){
                e.abbreviation = message;
            }
            if(e.organization === null){
                e.organization.name = message;
            }
            if(e.createTime === '' || e.createTime === null){
                e.createTime = message;
            }else {
                e.createTime = dayjs(e.createTime).format('YYYY-MM-DD HH:mm:ss');
            }
            if(e.modifiedTime === '' || e.modifiedTime === null){
                e.modifiedTime = message;
            }else {
                e.modifiedTime = dayjs(e.modifiedTime).format('YYYY-MM-DD HH:mm:ss');
            }
        },
        reSearch(){
            let that = this;
            this.currentPage =1;
            that.$http('/post/search', 'POST', {
                name: that.searchName,
                parentId: that.value,
                pageNum: that.currentPage
            }, (resp) => {
                if (resp.data.code === 20000) {
                    let data = resp.data.data;
                    that.tableData = data.list;
                    if (data.list.length > 0) {
                        let message = '暂无'
                        this.curType = that.tableData[0].type;
                        for (let e of that.tableData) {
                            this.postInfo(e,message)
                        }
                        that.currentPage = resp.data.data.currentPage;
                        that.pageSize = resp.data.data.pageSize;
                        that.total = resp.data.data.total;
                    } else {
                        that.$message({
                            type: 'info',
                            message: '未搜索到结果，请重试！'
                        });
                    }
                }
            })
        },
        //搜索
        search(){
            let that = this;
            this.isSearch = true;
            if (that.searchName === '') {
                that.$message({
                    type: 'error',
                    message: '请先填写需要搜索的名称'
                });
                this.isSearch = false;
            }
            that.$http('/post/search','POST',{
                name:that.searchName,
                organizationId:that.value,
                pageNum: that.currentPage
            },(resp)=>{
                if(resp.data.code === 20000){
                    let data = resp.data.data;
                    that.tableData = data.list;
                    if(data.list.length > 0){
                        let message = '暂无'
                        this.curOrganization = that.tableData[0].type;
                        for(let e of that.tableData){
                            this.postInfo(e,message);
                        }
                        that.currentPage = resp.data.data.currentPage;
                        that.pageSize = resp.data.data.pageSize;
                        that.total = resp.data.data.total;
                    }else {
                        that.reSearch();
                    }
                }
            })
        },
        //禁用岗位
        disablePost(id){
          let that = this;
          that.$confirm(`确认禁用该岗位吗？`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(()=>{
              that.$http('/post/enable','PUT',{id:id},(resp)=>{
                  if (resp.data.code === 20000){
                      that.$message({
                          message: '禁用成功',
                          type: 'success'
                      });
                      that.loadPostData();
                  }
              })
          }).catch(()=>{})
        },
        //修改岗位
        updatePost(id){
            let that = this;
            that.$http('/post/info','POST',{id:id},(resp)=>{
                if (resp.data.code === 20000){
                    let data = resp.data.data;
                    that.updatePostForm.id = data.id;
                    that.updatePostForm.name = data.name;
                    that.updatePostForm.abbreviation = data.abbreviation;
                    that.updatePostForm.organizationId = data.organizationId;
                }
            })
            that.editFormVisibleForUpdatePostData = true;
        },
        //修改岗位数据
        updatePostData(){
          let that = this;
          that.$confirm(`确认修改该岗位吗？`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
          }).then(()=>{
              that.$http('/post/updatePost','PUT',that.updatePostForm,(resp)=>{
                  if (resp.data.code === 20000){
                      that.$message({
                          message: '修改成功',
                          type: 'success'
                      });
                      that.editFormVisibleForUpdatePostData = false;
                      that.loadPostData()
                  }else {
                      that.$message({
                          message: '修改失败',
                          type: 'error'
                      });
                  }
              })
          }).catch(()=>{

          })
        },
        //添加岗位
        comitPost(){
            let that = this;
            that.$confirm(`确认添加该岗位吗？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(()=>{
                that.addPostForm.organizationId = that.value;
                that.$http('/post/addPost','POST',that.addPostForm,(resp)=>{
                    if (resp.data.code === 20000){
                        that.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        that.editFormVisibleForAddPostData = false;
                        that.loadPostData()
                    }else {
                        that.$message({
                            message: '添加失败',
                            type: 'error'
                        });
                    }
                })
            }).catch(()=>{})
        },
        //添加岗位
        addPost(){
            let that = this;
            that.editFormVisibleForAddPostData = true;
        },
        //处理选择变化
        handleChange(){
            this.loadPostData()
            console.log(this.value)
        },
        //加载组织数据
        loadOptions(){
            let that = this;
            that.$http('/organization/groupOrganization','POST',null,(resp)=>{
                if(resp.data.code === 20000){
                    let data = resp.data.data;
                    if (data.length>0){
                        for (let i = 0; i < data.length; i++) {
                            let children = [];
                            if (data[i].options.length>0){
                                for (let j = 0; j < data[i].options.length; j++) {
                                    children.push({
                                        value: data[i].options[j].id,
                                        label: data[i].options[j].name
                                    })
                                }
                            }
                            that.options.push({
                                label: data[i].label+"直属",
                                options: children
                            })
                        }
                    }
                }
                this.value = this.options[0].options[0].value;
                this.loadPostData()
            })
        },
        // 加载岗位数据
        loadPostData(){
            this.dataListLoading = true;
            let that = this;
            that.$http('/post','POST',{
                page: this.currentPage,
                organizationId: this.value,
            },(resp)=>{
                if(resp.data.code === 20000){
                    let data = resp.data.data;
                    that.tableData = data.list;
                    if(data.list.length <= 0){
                        this.currentPage = 1;
                        this.loadDatas()
                    }else {
                        let message = '暂无'
                        this.curOrganization = that.tableData[0].type;
                        for(let e of that.tableData){
                           this.postInfo(e,message);
                        }
                    }
                }
                that.currentPage = resp.data.data.currentPage;
                that.pageSize = resp.data.data.pageSize;
                that.total = resp.data.data.total;
                this.dataListLoading = false;
            })
        },
        //加载数据
        loadDatas(){
            let that = this;
            that.$http('/post','POST',{
                    pageNum: this.currentPage,
                    rootId: this.value
                },(resp)=> {
                if (resp.data.code === 20000) {
                    let data = resp.data.data;
                    that.tableData = data.list;
                    if (data.list.length <= 0) {
                        this.currentPage = 1;
                    } else {
                        let message = '暂无'
                        this.curType = that.tableData[0].type;
                        for (let e of that.tableData) {
                            this.postInfo(e, message);
                        }
                    }
                }
                that.currentPage = resp.data.data.currentPage;
                that.pageSize = resp.data.data.pageSize;
                that.total = resp.data.data.total;
            })
        },
        // 切换分页
        changePage(page) {
            this.currentPage = page;
            if(this.isSearch){
                this.search();
            }else{
                this.loadPostData();
            }

        }
    }
}
</script>
<style lang="less">
.table {
  margin-top: 20px;
}
</style>